package com.yurun.controller;

import com.yurun.common.Result;
import com.yurun.entity.Course;
import com.yurun.service.CourseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 宫静雨
 * @since 2022-08-29 15:45:34
 */
@RestController
public class CourseController {

    @Resource
    private CourseService courseService;

    @PostMapping("/YR100304")
    public Result add(@RequestBody Course course) {
        if (Objects.isNull(course.getCourseId()) ||
                Objects.isNull(course.getCourseName())) {
            throw new IllegalArgumentException();
        }
        return courseService.save(course) ?
                Result.res("000000", "Succeeded") :
                Result.res("000001", "Fail");
    }

    @PostMapping("/YR100303")
    public Result modify(@RequestBody Course course) {
        if (Objects.isNull(course.getCourseId())) {
            throw new IllegalArgumentException();
        }
        return courseService.updateById(course) ?
                Result.res("000000", "Succeeded") :
                Result.res("000001", "Fail");
    }

    @GetMapping("/YR100302")
    public Result getById(@RequestParam(name = "courseId") Integer courseId) {
        return Result.res("000000", "Succeeded")
                .setData(courseService.getById(courseId));
    }

    @GetMapping("/YR100301")
    public Result list(@RequestParam(required = false) List<Integer> courseId,
                       @RequestParam(required = false) String courseName) {
        final List<Course> courses = courseService.listCourse(courseId, courseName);
        return Result.res("000000", "Succeeded")
                .setData(courses);
    }
}
